#include "MTPDevice.h"
#include "MTPDeviceException.h"
#include "stdafx.h"
#include "mswmdm_i.c"
#include "mswmdm.h"
#include "sac.h"
#include "scclient.h"

using namespace System;
using namespace Collections;

MTPDevice::MTPDevice(void *device)
{
	m_pDevice = device;
	getDeviceName();
	getManufacturer();
	getDesription();
}

MTPDevice::~MTPDevice()
{

}

void MTPDevice::getManufacturer()
{
	IWMDMDevice3* pDevice = (IWMDMDevice3*)m_pDevice;
	wchar_t pwsString[256];
	HRESULT hr = pDevice->GetManufacturer(pwsString, 256);
	m_deviceManufacturer = gcnew String(pwsString);
}

void MTPDevice::getDesription()
{
	IWMDMDevice3* pDevice = (IWMDMDevice3*)m_pDevice;
	wchar_t pwsString[256];
	HRESULT hr = pDevice->GetCanonicalName(pwsString, 256);
		if SUCCEEDED(hr) {
			m_deviceDescription = gcnew String(pwsString);
		} else
		{
			throw gcnew MTPDeviceException();
		}
}


void MTPDevice::getDeviceName()
{
	IWMDMDevice3* pDevice = (IWMDMDevice3*)m_pDevice;
	wchar_t pwsString[256];
	HRESULT hr = pDevice->GetName(pwsString, 256);

	if SUCCEEDED(hr) {
		m_deviceFriendlyName = gcnew String(pwsString);
	} else
	{
		throw gcnew MTPDeviceException();
	}

}

DeviceContent^ MTPDevice::enumerateContent()
{
	return gcnew DeviceContent();
}

/*void MTPDevice::getStorageInfo()
{	
	// get storage info
	DWORD tempDW;
	pIDevice->GetType(&tempDW);
	if (tempDW & WMDM_DEVICE_TYPE_STORAGE) {
		IWMDMEnumStorage *pIEnumStorage = NULL;
		IWMDMEnumStorage *pIEnumFileStorage = NULL;
		IWMDMEnumStorage *pIEnumFileStorageL2 = NULL;
		IWMDMStorage *pIStorage = NULL;
		IWMDMStorage3 *pIFileStorage = NULL; 
		IWMDMStorage3 *pIFileStorage_L2 = NULL;
		_WAVEFORMATEX format;
		hr = pIDevice->EnumStorage(&pIEnumStorage);
		if SUCCEEDED(hr) {
			pIEnumStorage->Reset();
			hr = pIEnumStorage->Next(1, (IWMDMStorage **)&pIStorage, &ulNumFetched);
			while (SUCCEEDED(hr) && (hr != S_FALSE)) {
				hr = pIStorage->EnumStorage(&pIEnumFileStorage);
				if SUCCEEDED(hr) {
					pIEnumFileStorage->Reset();
					hr = pIEnumFileStorage->Next(1, (IWMDMStorage **)&pIFileStorage, &ulNumFetched);

					while (SUCCEEDED(hr) && hr != S_FALSE) {
						hr = pIFileStorage->GetName(pwsString, 256); 
						hr = pIFileStorage->GetAttributes(&tempDW, &format);
						if SUCCEEDED(hr) {
							if (tempDW & WMDM_FILE_ATTR_FOLDER) {															
								if (wcscmp(pwsString, L"Music") == 0) {
									IWMDMStorage3 *pNewStorage;
									hr = pIFileStorage->QueryInterface(IID_IWMDMStorage3, (void **)&pNewStorage);
									if SUCCEEDED(hr) {
										IWMDMStorageControl3 *pIWMDMStorageControl;
										hr = pNewStorage->QueryInterface(IID_IWMDMStorageControl3, 
											(void**)&pIWMDMStorageControl);

										if SUCCEEDED(hr)
										{
											IWMDMMetaData *pIWMDMMetaData = NULL;
											hr = pNewStorage->CreateEmptyMetadataObject(&pIWMDMMetaData);
											if (SUCCEEDED(hr)) {
												DWORD dw = WMDM_FORMATCODE_MP3;
												hr = pIWMDMMetaData->AddItem(WMDM_TYPE_DWORD, g_wszWMDMFormatCode, (BYTE *)&dw, sizeof(dw));
												hr = pIWMDMMetaData->AddItem(WMDM_TYPE_STRING, g_wszWMDMTitle, (BYTE *)L"TSOS-05-23-2006", 32);
												hr = pIWMDMMetaData->AddItem(WMDM_TYPE_STRING, g_wszWMDMAlbumTitle, (BYTE *)L"www.skinnyonsports.com", 46);
												hr = pIWMDMMetaData->AddItem(WMDM_TYPE_STRING, g_wszWMDMAuthor, (BYTE *)L"The Skinny on Sports", 42);
												hr = pIWMDMMetaData->AddItem(WMDM_TYPE_STRING, g_wszWMDMGenre, (BYTE *)L"Podcast", 16);
												hr = pIWMDMMetaData->AddItem(WMDM_TYPE_STRING, g_wszWMDMYear, (BYTE *)L"2006", 10);
												dw = 0;
												hr = pIWMDMMetaData->AddItem(WMDM_TYPE_DWORD, g_wszWMDMTrack, (BYTE *)&dw, sizeof(dw));
												DWORD ow[2];
												ow[0] = 0x77825f00;
												ow[1] = 1;
												hr = pIWMDMMetaData->AddItem(WMDM_TYPE_QWORD, g_wszWMDMDuration, (BYTE *)ow, 2 * sizeof(dw));
												ow[0] = 0x7532e5;
												ow[1] = 0;
												hr = pIWMDMMetaData->AddItem(WMDM_TYPE_QWORD, g_wszWMDMFileSize, (BYTE *)ow, 2 * sizeof(dw));
												if (SUCCEEDED(hr)) {
													IWMDMStorage *pNewObject = NULL;

													hr = pIWMDMStorageControl->Insert3(
														WMDM_MODE_BLOCK | WMDM_CONTENT_FILE, 
														0, 
														L"C:\\Music\\testfile.mp3", 
														NULL, 
														NULL, 
														NULL,
														pIWMDMMetaData,
														NULL,
														(IWMDMStorage **)&pNewObject);

													if SUCCEEDED(hr)
														wprintf(L"Insert Succeeded\n");
												}
											}
										}
									}

								}


							} else {
								printf("\n");
							}
						} else {
							printf("\n");
						}

						pIFileStorage->Release();

						hr = pIEnumFileStorage->Next(1, (IWMDMStorage **)&pIFileStorage, &ulNumFetched);
					}
					pIEnumFileStorage->Release();
				}


				hr = pIEnumStorage->Next(1, (IWMDMStorage **)&pIStorage, &ulNumFetched);
			}
			pIEnumStorage->Release();
		}
	}
}*/